package com.rzico.sbl.ui.home

import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityMarketGroupBinding
import com.rzico.sbl.model.CommonData
import com.xinnuo.common.extend.conditionFalse
import com.xinnuo.common.extend.startActivityEx
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.slimadapter.SlimAdapter

class MarketGroupActivity : BaseActivity(R.layout.activity_market_group) {

    private val mBinding by lazy { ActivityMarketGroupBinding.bind(getRootView()) }

    override fun initData() {
        initTitle(title = "活动列表")

        initTabLayout()
        initLayout()

        repeat(10) {
            mList += CommonData(it.toString())
        }

        mAdapter.setDataList(mList)
    }

    private fun initTabLayout() {
        mBinding
            .groupTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.let { }
                    }
                }

                addTab(this.newTab().setText("进行中"), true)
                addTab(this.newTab().setText("已关闭"), false)
            }
    }

    private fun initLayout() {
        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyHint.text = "暂无相关活动信息！"
                }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<CommonData>(R.layout.item_market_group_list) { jector, bean, _ ->
                            jector.apply {
                                clicked(R.id.item_group_edit) { startActivityEx<MarketGroupEditActivity>() }

                                clicked { startActivityEx<MarketGroupRecordActivity>() }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

}